# -*- coding: utf-8 -*-
from datetime import timedelta
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator

#Hive表名
hive_table = "dwd_spmi_composite_bill_hi"
#任务名
task_id = f"spmi_dwd__{hive_table}"

spmi_dwd__dwd_spmi_composite_bill_hi = SparkSqlOperator(
    task_id=f'{task_id}',
    task_concurrency=1,
    pool_slots=2,
    master='yarn',
    name=f'{task_id}_{{{{ execution_date | cst_hour }}}}',
    sql=f'spmi_hedge_hi/dwd/{hive_table}/execute.sql',
    retries=0,
    pool='spmi_piece',
    email=['shenjiaming@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    execution_timeout=timedelta(minutes=60),
    yarn_queue='pro',
    driver_memory='2G',
    driver_cores=2,
    executor_cores=6,
    executor_memory='16G',
    num_executors=80,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 80,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 720,  # 当某个缓存数据的executor空闲时间超过这个设定值，就会被kill
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 1400,
          'spark.shuffle.consolidateFiles': 'true',
          'spark.shuffle.memoryFraction': '0.8',
          'spark.shuffle.file.buffer':'64k',
          'spark.executor.extraJavaOptions': '-XX:+UseG1GC -XX:ParallelGCThreads=6 -XX:ConcGCThreads=2',
          'spark.dynamicAllocation.executorIdleTimeout': 720, #当某个executor空闲超过这个设定值，就会被kill，默认60s
          'spark.shuffle.io.maxRetries': 72,
          'spark.shuffle.io.retryWait': 10 ,
          'spark.network.timeout':600,
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': 100,
              'hive.exec.max.dynamic.partitions.pernode': 100,
              },
)
